package com.jin10.controller;

import cn.hutool.core.util.StrUtil;
import com.jin10.bean.BaseResponse;
import com.jin10.dto.ParagraphDto;
import com.jin10.service.ISentenceLibraryService;
import com.jin10.vo.TagTreeVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author raofy
 * @date 2022-01-20 15:15
 * @desc
 */
@RestController
@RequestMapping("paragraph")
public class ParagraphController {

    @Autowired
    ISentenceLibraryService iSentenceLibraryService;

    @PostMapping("automation")
    public BaseResponse automation(@RequestBody ParagraphDto dto) {
        Object sentenceLibraryByTag = iSentenceLibraryService.getSentenceByTag(dto);
        BaseResponse ok = BaseResponse.ok(sentenceLibraryByTag);
        return ok;
    }

    @PostMapping("automationForTitle")
    public BaseResponse automationForTitle(@RequestBody ParagraphDto dto) {
        List<TagTreeVo> sentenceLibraryByTag = (List<TagTreeVo>) iSentenceLibraryService.getSentenceByTag(dto);

        return BaseResponse.ok(diGuiShengChengWenZhang(sentenceLibraryByTag, new StringBuffer()));
    }

    public List<String> diGuiShengChengWenZhang(List<TagTreeVo> sentenceLibraryByTag, StringBuffer stringBuffer) {
        if (!sentenceLibraryByTag.isEmpty()) {
            ArrayList<String> titles = new ArrayList<>();
            for (TagTreeVo tagTreeVo : sentenceLibraryByTag) {
                stringBuffer.append(tagTreeVo.getTitle());
                if (tagTreeVo.getSubclass() != null && !tagTreeVo.getSubclass().isEmpty()) {
                    returnTitle(tagTreeVo, stringBuffer);
                }
                titles.add(stringBuffer.toString());
                stringBuffer = new StringBuffer();
            }
            return titles;
        } else {
            return new ArrayList<>();
        }
    }

    public void returnTitle(TagTreeVo tagTreeVo, StringBuffer stringBuffer) {
        stringBuffer.append(tagTreeVo.getTitle());
        List<TagTreeVo> subclass = tagTreeVo.getSubclass();
        if (subclass != null && !subclass.isEmpty()) {
            for (TagTreeVo treeVo : subclass) {
                returnTitle(treeVo, stringBuffer);
            }
        }

    }

}
